package com.test.pack1.demo.base1_5;

/**
 * Created by dws on 2017/12/4.
 * 加权 查询算法
 */

public class WeightedQuickUnionUF {
    private int[] id;
    private int[] sz;
    private int count;

    public WeightedQuickUnionUF(int N) {
        count = N;
        id = new int[N];
        for (int i = 0; i < N; i++) {
            id[i]=i;
        }
        sz = new int[N];
        for (int i = 0; i < N; i++) {
            sz[i]=i;
        }
    }
    public int count(){
        return count;
    }

    public boolean connected(int p,int q){return find(p)==find(q);}

    public int find(int p){
        while(p!=id[p])p=id[p];
        return p;
    }

    public void union(int p,int q){
        int i = find(p);
        int j = find(q);
        if(i==j)return;
        if (sz[i]<sz[j]){
            id[i]=j;
            sz[j]+=sz[i];
        }else {
            id[j]=i;
            sz[i]+=sz[j];
        }
        count--;
    }

    public int[] getId() {
        return id;
    }

    public void setId(int[] id) {
        this.id = id;
    }

    public int[] getSz() {
        return sz;
    }

    public void setSz(int[] sz) {
        this.sz = sz;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
